package com.white.mysoundrecorder;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.Toast;

import java.io.File;

/**
 * Created by yutf on 2017/8/30 0030.
 */

public class RecordFragment extends Fragment
{

    private FloatingActionButton floatingActionButton;
    private Chronometer cr;
    private RecordEngine engine;
    private Watch watch;

    public static RecordFragment getInstance()
    {
        RecordFragment frag = new RecordFragment();
        return frag;
    }

    private Context mContext;
    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
        mContext = context;
    }

    private boolean hasPermission;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        prepareEngine();
    }

    private void prepareEngine()
    {
        File f = mContext.getExternalFilesDir(null);
        if(f == null)
        {
            f = mContext.getFilesDir();
        }
        File f2 = new File(f, "record");
        if(!f2.exists())
        {
            f.mkdirs();
        }
        engine = RecordEngine.getInstance(mContext, f2.getAbsolutePath());
        engine.setUiCallback(new RecordEngine.UiCallback()
        {
            @Override
            public void available()
            {
                available = true;
            }

            @Override
            public void onStart()
            {
                floatingActionButton.setImageResource(R.drawable.stop);
                cr.setBase(SystemClock.uptimeMillis());
                cr.start();
                Toast.makeText(mContext, "", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStop()
            {
                cr.stop();
                cr.setBase(SystemClock.uptimeMillis());
                floatingActionButton.setImageResource(R.drawable.start);
            }
        });
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.frag_record, null);
        findViews(v);
        return v;
    }

    private boolean toggleRecord;
    private boolean available;
    private void findViews(View v)
    {
        floatingActionButton = (FloatingActionButton) v.findViewById(R.id.fac);
        cr = (Chronometer) v.findViewById(R.id.cr);
        watch = (Watch) v.findViewById(R.id.watch);
        watch.start();
        cr.setFormat("MM:SS");
        floatingActionButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
//                if(!available)
//                {
//                    Toast.makeText(mContext, "正在准备中...", Toast.LENGTH_SHORT).show();
//                    return;
//                }

                toggleRecord = !toggleRecord;

                if(toggleRecord)
                {
                    watch.pause();
//                    checkPermis();
                }else
                {
                    watch.resume();
//                    if(engine != null)
//                    {
//                        engine.stop();
//                    }
                }
            }
        });
    }

    public void checkPermis()
    {
        if(ActivityCompat.checkSelfPermission(mContext, Manifest.permission.RECORD_AUDIO)
                != PackageManager.PERMISSION_GRANTED)
        {
            requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, 0);
        }else
        {
            engine.start();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    {
        if(requestCode == 0)
        {
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                engine.start();
            }
        }
    }

    @Override
    public void onDestroy()
    {
        if(engine != null)
        {
            engine.destroy();
        }
        super.onDestroy();
    }
}
